package com.aaa.multithreading.demo03;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: 使用多线程模拟车站中四个窗口同时出售100张车票
 * @date 2022/10/31
 */
public class SellTickets implements Runnable{
    //创建手动锁对象
    Lock lock = new ReentrantLock();
    /**
     * 模拟的100张票
     */
    private int ticket=100;


    @Override
    public void run() {

            while (true){
                try {
                    lock.lock();
                Thread.sleep(10);
                if(ticket>0){
                    System.out.println(Thread.currentThread().getName()+"出售一张票"+--ticket);
                }else {
                    break;
                }
            } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
        }

    }
}
